package CDLibrary;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.event.ActionListener;

public class BuscarReg extends JDialog {

	//private final JPanel contentPanel = new JPanel();
	private static final long serialVersionUID = 1L;
	private CD ventanaPadre;
	private JButton jbtAcceptar;
	private JButton jbtCancelar;
	private JButton jbtBorrar;
	private JList jlsListaCDs;
	

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			BuscarReg dialog = new BuscarReg();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	private void initGUI() {	
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Buscar Registres");
			this.setResizable(false);
			pack();
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
 private void construeixGUI (){

	 JScrollPane jScrollPane1 = new JScrollPane();
		jScrollPane1.setBounds(17, 21, 247, 220);
		getContentPane().add(jScrollPane1);		
		jlsListaCDs = new JList();			
		jScrollPane1.setViewportView(jlsListaCDs);
		
		jbtAcceptar = new JButton("Acceptar");
		jbtAcceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int i=jlsListaCDs.getSelectedIndex();
				if (i>=0)
					ventanaPadre.setRegDatos(i);
				
				
			}
		});
		jbtAcceptar.setMnemonic('A');
		jbtAcceptar.setBounds(311, 60, 89, 23);
		getContentPane().add(jbtAcceptar);
		
		jbtCancelar = new JButton("Cancelar");
		jbtCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tancarFinestra();
			}			
		});
		jbtCancelar.setBounds(311, 105, 89, 23);
		getContentPane().add(jbtCancelar);
		
		jbtBorrar = new JButton("Borrar");
		jbtBorrar.setMnemonic('B');
		jbtBorrar.setBounds(311, 156, 89, 23);
		getContentPane().add(jbtBorrar);
 }
	
	protected void tancarFinestra() {				
			this.setVisible(false);
			this.dispose();		
}
	public void actualitzarLlista() {
		jlsListaCDs.setListData(ventanaPadre.getCDs());
	}
	public BuscarReg(JFrame frame) {
		super(frame);
		setResizable(false);
		initGUI();
		ventanaPadre=(CD)frame;
		getContentPane().setLayout(null);
		construeixGUI ();
		jlsListaCDs.setListData(ventanaPadre.getCDs());			
	}
	
	}

